package com.huqing.icu.utils;

import org.apache.commons.lang.time.DateFormatUtils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author KingsHunter
 * @createDate June 20th,2016
 */
public final class DateUtils {
    public static final String yyyyMMddHHmmss = "yyyyMMddHHmmss";

    public static final String yyyyMMdd_HHmmss = "yyyy-MM-dd HH:mm:ss";

    private static final String yyyyMM = "yyyyMM";

    public static final String yyyyMMdd = "yyyyMMdd";

    public static final String yyyyMMDDWithBar = "yyyy-MM-dd";

    public static final String yyyyMMWithBar = "yyyy-MM";

    public static final String yyyyWithBar = "yyyy";

    //只显示时分，比如14:22
    public static final String HHmm = "HH:mm";

    public static final String HH = "HH";

    public static final String DD = "dd";

    public static final String MM = "MM";

    //只显示月日，比如01-22
    public static final String MMDD = "MM-dd";

    //只显示月日时分，比如 09-07 08:50
    public static final String MMdd_HHmm = "MM-dd HH:mm";

    //只显示月日时分，比如 09-07 08:50
    public static final String yyyyMMdd_HHmm = "yyyy-MM-dd HH:mm";

    /**
     * 20181201120100
     *
     * @return
     */
    public static String getCurYyyyMMddHHmmss() {
        return DateFormatUtils.format(System.currentTimeMillis(), yyyyMMddHHmmss);
    }

    public static String getCurYyyyMMdd() {
        return DateFormatUtils.format(System.currentTimeMillis(), yyyyMMDDWithBar);
    }

    public static String getCurYyyyMM() {
        return DateFormatUtils.format(System.currentTimeMillis(), yyyyMMWithBar);
    }

    public static String getCurHH() {
        return DateFormatUtils.format(System.currentTimeMillis(), HH);
    }

    public static String getMM(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        Date nextMonth = calendar.getTime();
        return DateFormatUtils.format(nextMonth, MM);
    }

    public static String getYyyyMM(Date date, Integer num) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // 将当前时间加/减num个月
        calendar.add(Calendar.MONTH, num);
        Date nextMonth = calendar.getTime();
        return DateFormatUtils.format(nextMonth, yyyyMMWithBar);
    }

    public static String getYyyyMM(long timeMillis) {
        return DateFormatUtils.format(timeMillis, yyyyMM);
    }

    public static long currentTimeInMills() {
        return System.currentTimeMillis();
    }

    public static long currentTimeInSeconds() {
        return currentTimeInMills() / 1000;
    }

    /**
     * 计算两个日期之间相差的天数
     *
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(Date smdate, Date bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        smdate = sdf.parse(sdf.format(smdate));
        bdate = sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(between_days));
    }

    public static int daysBetween(long timeMills1, long timeMills2) {
        if (timeMills1 > timeMills2)
            return 0;
        long timeMills = timeMills2 - timeMills1;
        Long daysBetween = Math.abs(timeMills / (24 * 60 * 60 * 1000));
        return daysBetween.intValue();
    }

    public static int secondsBetweenFutureTimeMills(long timeMills) {
        long currentTimeMills = currentTimeInMills();
        if (currentTimeMills >= timeMills)
            return 0;
        long diff = timeMills - currentTimeMills;
        Long secondsBetween = Math.abs(diff / 1000);
        return secondsBetween.intValue();
    }

    public static int secondsBetween(long timeMills1, long timeMills2) {
        if (timeMills1 > timeMills2)
            return 0;
        long timeMills = timeMills2 - timeMills1;
        Long secondsBetween = Math.abs(timeMills / 1000);
        return secondsBetween.intValue();
    }

    public static boolean isEqualTimeRange(int date, int hour) {
        Calendar c = Calendar.getInstance(Locale.CHINA);
        int dayOfMonth = c.get(Calendar.DATE);
        int hourOfDay = c.get(Calendar.HOUR_OF_DAY);
        if (date == dayOfMonth && hour == hourOfDay)
            return true;
        return false;
    }

    private static int time1CompareTotime2(LocalTime time1, LocalTime time2) {
        return time1.compareTo(time2);
    }

    public static int circlePerDaySecondsBetween(int secondOfDay) {
        LocalTime time1 = LocalTime.now();
        LocalTime time2 = LocalTime.ofSecondOfDay(secondOfDay);
        int compare = time1CompareTotime2(time1, time2);
        if (compare == -1) {
            //当前时间未达到预定时间
            int currentSecond = time1.toSecondOfDay();
            int diff = secondOfDay - currentSecond;
            return diff;
        } else if (compare == 0) {
            //当前时间等于预定时间
            return 24 * 3600;
        } else if (compare == 1) {
            //当前时间已超过预定时间
            int currentSecond = time1.toSecondOfDay();
            int diff = secondOfDay - currentSecond;
            return 24 * 3600 + diff;
        }
        return 0;
    }

    public static boolean isTheSameYearMonth(int yyyyMMDateValue) {
        Calendar c = Calendar.getInstance(Locale.CHINA);
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        Integer currentyyyyMMValue = Integer.valueOf(String.valueOf(year + (++month)));
        if (currentyyyyMMValue == yyyyMMDateValue)
            return true;
        return false;
    }

    public static Long addSpecifyMinute(long timeInMills, int minute) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTimeInMillis(timeInMills);
        calendar.add(Calendar.MINUTE, minute);
        return calendar.getTimeInMillis();
    }

    public static Long addThirteenMonth(long timeInMills) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTimeInMillis(timeInMills);
        calendar.add(Calendar.MONTH, 13);
        calendar.set(Calendar.DAY_OF_MONTH, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    public static Integer getCurrentDayOfWeek(long currentTimeInMills) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTimeInMillis(currentTimeInMills);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        return dayOfWeek == 1 ? 7 : --dayOfWeek;
    }

    public static Long getNextWeekDateTime(int date) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.set(Calendar.DAY_OF_WEEK, date);
        calendar.set(Calendar.HOUR_OF_DAY, 8);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.add(Calendar.WEEK_OF_YEAR, 1);
        return calendar.getTimeInMillis();
    }

    public static Long getCurrentWeekDateTime(int date) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.set(Calendar.DAY_OF_WEEK, date);
        calendar.set(Calendar.HOUR_OF_DAY, 8);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取第month月之前的time
     *
     * @param month
     * @return
     */
    public static Long getMonthDateTimeAgo(Integer month) {
        if (month == null) return null;
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.add(Calendar.MONTH, -month);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取第month月之后的time
     *
     * @param month
     * @return
     */
    public static Long getMonthDateTimeLater(Integer month) {
        if (month == null) return null;
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.add(Calendar.MONTH, month);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * 从当前时间到第nDay的nhour点时，还差多少秒
     * 如nday = 0, nhour = 4，则如果在当日4点前调用，则返回到当日4点还有多少s，如果在4点后调用 ，则返回到次日4点还有多少s
     *
     * @param nDay
     * @param nHour
     * @return
     */
    public static int getNextTimeTtl(int nDay, int nHour) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        int hours = calendar.get(Calendar.HOUR_OF_DAY);
        if (hours < nHour) {
            calendar.add(Calendar.DATE, nDay);
        } else {
            calendar.add(Calendar.DATE, nDay + 1);
        }
        calendar.set(Calendar.HOUR_OF_DAY, nHour);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        long curTime = System.currentTimeMillis();
        int ttl = (int) (calendar.getTimeInMillis() - curTime) / 1000;
        return ttl;
    }

    public static int getSecondOfDay(int day) {
        return day * 24 * 60 * 60;
    }


    public static Long addSpecifyDay(long timeInMills, int days) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTimeInMillis(timeInMills);
        calendar.add(Calendar.DAY_OF_YEAR, days);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }


    /**
     * 根据日期格式对日期转换字符串
     *
     * @param format 日期格式
     * @param date   日期
     * @return 当前日期格式的字符串
     */
    public static String getFormatStringFromDate(String format, Date date) {
        return new SimpleDateFormat(format).format(date);
    }

    /**
     * 获取日期当天的开始时间
     *
     * @param dateString 日期
     * @return 时间戳
     */
    public static Timestamp getStartOfDayTimestamp(String dateString) {
        // 将日期字符串解析为LocalDate对象
        LocalDate date = LocalDate.parse(dateString);
        // 获取当天最早开始时间
        ZonedDateTime startOfDay = date.atStartOfDay(ZoneId.systemDefault());
        // 将最早开始时间转换为Timestamp对象并返回
        return Timestamp.from(startOfDay.toInstant());
    }

    /**
     * 获取日期当天的结束时间
     *
     * @param dateString 日期
     * @return 时间戳
     */
    public static Timestamp getEndOfDayTimestamp(String dateString) {
        // 将日期字符串解析为LocalDate对象
        LocalDate date = LocalDate.parse(dateString);
        // 获取当天最晚时间
        ZonedDateTime endOfDay = date.atTime(LocalTime.MAX).atZone(ZoneId.systemDefault());
        // 将最晚时间转换为Timestamp对象并返回
        return Timestamp.from(endOfDay.toInstant());
    }

    /**
     * 将字符串转为Timestamp
     *
     * @param dateString
     * @return
     */
    public static Timestamp convertStringToTimestamp(String format, String dateString) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return new Timestamp(dateFormat.parse(dateString).getTime());
    }

    /**
     * 获取两个时间戳之前的所有日期
     *
     * @param start
     * @param end
     * @return
     */
    public static List<String> getDatesBetween(Timestamp start, Timestamp end) {
        // 将Timestamp转换为Instant和ZoneId对象
        Instant startInstant = start.toInstant();
        Instant endInstant = end.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();

        // 将起始和结束时间转换为LocalDate对象
        LocalDate startDate = startInstant.atZone(zoneId).toLocalDate();
        LocalDate endDate = endInstant.atZone(zoneId).toLocalDate();

        // 创建一个列表，保存两个日期之间的所有日期
        List<String> dates = new ArrayList<>();
        LocalDate currentDate = startDate;
        while (!currentDate.isAfter(endDate)) {
            dates.add(currentDate.toString());
            currentDate = currentDate.plusDays(1);
        }

        return dates;
    }

    /**
     * 获得当天0点时间
     *
     * @return
     */
    public static Date getTimesDayMorning() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获得当天24点时间
     *
     * @return
     */
    public static Date getTimesDayNight() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /**
     * 获得本周第一天0点时间
     *
     * @return
     */
    public static Date getTimesWeeklyMorning() {
        Calendar cal = Calendar.getInstance(Locale.CHINA);
        cal.setFirstDayOfWeek(Calendar.MONDAY);//以周一为首日
        cal.setTime(new Date());
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);//周一
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获得本周最后一天24点
     *
     * @return
     */
    public static Date getTimesWeeklyNight() {
        Calendar cal = Calendar.getInstance(Locale.CHINA);
        cal.setFirstDayOfWeek(Calendar.MONDAY);//以周一为首日
        cal.setTime(new Date());
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);//周日
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);
        return cal.getTime();
    }


    /**
     * 字符串转成日期
     *
     * @param format  日期格式
     * @param dateStr 日期字符串
     * @return 当前日期格式的字符串
     * @throws ParseException
     */
    public static Date strToDate(String format, String dateStr) throws ParseException {
        return new SimpleDateFormat(format).parse(dateStr);
    }

    public static String strToStr(String dateStr, String format) throws ParseException {
        Date date = strToDate(DateUtils.yyyyMMDDWithBar, dateStr);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }

    public static Date strToDateV2(String format, String dateStr) {
        Date date = null;
        try {
            date = new SimpleDateFormat(format).parse(dateStr);
        } catch (ParseException e) {
            throw new RuntimeException("时间解析错误");
        }
        return date;
    }

    /**
     * 获取当前时间一定天数后的日期，
     *
     * @param queryDate 查询日期
     * @param days      天数，正数为+，负数为-
     * @return
     */
    public static String getDateByDays(String queryDate, int days) throws ParseException {
        Date date = DateUtils.strToDate(DateUtils.yyyyMMDDWithBar, queryDate);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int day = c.get(Calendar.DATE) + days;
        c.set(Calendar.DATE, day);
        return DateUtils.getFormatStringFromDate(DateUtils.yyyyMMDDWithBar, c.getTime());
    }

    /**
     * 获取当前时间一定天数后的日期，
     *
     * @param queryDate 查询日期
     * @param days      天数，正数为+，负数为-
     * @return 返回 年-月-日 时-分-秒
     */
    public static String getDateByDaysV2(String queryDate, int days) throws ParseException {
        Date date = DateUtils.strToDate(DateUtils.yyyyMMdd_HHmmss, queryDate);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int day = c.get(Calendar.DATE) + days;
        c.set(Calendar.DATE, day);
        return DateUtils.getFormatStringFromDate(DateUtils.yyyyMMdd_HHmmss, c.getTime());
    }

    /**
     * 获取当前时间一定天数后的日期，
     *
     * @param time 开始日期
     * @param days 天数，正数为+，负数为-
     * @return 返回 年-月-日 时-分-秒
     */
    public static Timestamp getTimestampByDays(Timestamp time, int days) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time.getTime());
        // 将当前时间加7天
        calendar.add(Calendar.DAY_OF_MONTH, days);
        return new Timestamp(calendar.getTime().getTime());
    }

    /**
     * 获取n小时之后的时间
     *
     * @param queryTime
     * @param hour
     * @return
     * @throws ParseException
     */
    public static Timestamp getTimeByHour(Timestamp queryTime, int hour) {
        LocalDateTime localDateTime = queryTime.toLocalDateTime();
        Duration duration = Duration.ofHours(hour);
        return Timestamp.valueOf(localDateTime.plus(duration));
    }

    /**
     * 判断日期是否是当天
     *
     * @param inputJudgeDate
     * @return
     */
    public static boolean isToday(Date inputJudgeDate) {
        boolean flag = false;
        // 获取当前系统时间
        long longDate = System.currentTimeMillis();
        Date nowDate = new Date(longDate);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(nowDate);
        String subDate = format.substring(0, 10);
        // 定义每天的24h时间范围
        String beginTime = subDate + " 00:00:00";
        String endTime = subDate + " 23:59:59";
        Date paseBeginTime = null;
        Date paseEndTime = null;
        try {
            paseBeginTime = dateFormat.parse(beginTime);
            paseEndTime = dateFormat.parse(endTime);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (inputJudgeDate.after(paseBeginTime) && inputJudgeDate.before(paseEndTime)) {
            flag = true;
        }
        return flag;
    }

    /**
     * 获得本月第一天0点时间
     *
     * @return
     */
    public static Date getTimesMonthMorning(Calendar cal) {
//		Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }


    /**
     * 获得本月最后一天24点时间
     *
     * @return
     */
    public static Date getTimesMonthNight(Calendar cal) {
//		Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, 1);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 1, 0, 0, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获取本月最后一天的日期
     *
     * @param cal
     * @return
     */
    public static Date getLastDayOfMonth(Calendar cal) {
//		Calendar cal = Calendar.getInstance();
        //cal.add(Calendar.MONTH, 1);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.getActualMaximum(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获得上个月第一天0点时间
     *
     * @return
     */
    public static Date getTimesLastMonthMorning(Calendar cal) {
//		Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(Calendar.MONTH, -1);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }


    //每月清算时间
    private static final int RESET_DAY_OF_MONTH = 1;

    private static final int RESET_HOUR_OF_DAY = 0;

    public static Timestamp[] getResetTimeInterval(Long timestamp) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        //去除毫秒数，保证是月初1日00:00:00 000
        calendar.setTimeInMillis(timestamp);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                RESET_DAY_OF_MONTH, RESET_HOUR_OF_DAY, 0, 0);

        Timestamp[] array = new Timestamp[2];

        Timestamp startTime = new Timestamp(calendar.getTimeInMillis());
        array[0] = startTime;

        calendar.add(Calendar.MONTH, 1);
        Timestamp endTime = new Timestamp(calendar.getTimeInMillis());
        array[1] = endTime;

        return array;
    }

    /**
     * 获取本月
     *
     * @return
     */
    public static int getYearOfMonth() {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        return calendar.get(Calendar.MONTH);
    }


    public static long getMonthExpireTime() {
        long timestamp = System.currentTimeMillis();
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        //去除毫秒数，保证是月初1日00:00:00 000
        calendar.setTimeInMillis(timestamp);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                RESET_DAY_OF_MONTH, RESET_HOUR_OF_DAY, 0, 0);
        //下一个月的0点0分
        calendar.add(Calendar.MONTH, 1);

        return (calendar.getTimeInMillis() - timestamp);
    }

    public static Long getDayExpireTime() {
        Long now = System.currentTimeMillis();
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        //去除毫秒数，保证是月初1日00:00:00 000
        calendar.setTimeInMillis(now);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        //下一天的0点0分
        calendar.add(Calendar.DATE, 1);

        return (calendar.getTimeInMillis() - now);
    }

    public static Timestamp getWithInDayTimestamp(Date time, int days) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String value = format.format(time);
        time = java.sql.Date.valueOf(value);
        return new Timestamp(time.getTime() - days * 24 * 60 * 60 * 1000);
    }

    public static LocalDateTime stringToLocalDateTime(String str, String format) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
        return LocalDateTime.parse(str, df);
    }

    public static String localDateTimeToStr(LocalDateTime time, String format) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
        return time.format(df);
    }

    public static String convertTime(Timestamp time, String code) {
        Date createDate = new Date(time.getTime());
        //获取今天0点的时候
        Timestamp today = getWithInDayTimestamp(new Date(), 0);
        //判断是不是今天
        boolean isToday = isToday(createDate);
        String createTimeStr = DateFormatUtils.format(time.getTime(), yyyyMMDDWithBar);
        if (isToday) {
            //如果是今天，则显示时分，比如14:22
            createTimeStr = DateFormatUtils.format(time.getTime(), HHmm);
            return createTimeStr;
        }

        //把时间往前推一天，就是昨天,获取昨天0点的时间
        Timestamp yesterday = getWithInDayTimestamp(today, 1);
        if (time.compareTo(yesterday) >= 0 && time.compareTo(today) < 0) {
            createTimeStr = "昨日 " + DateFormatUtils.format(time.getTime(), HHmm);
            return createTimeStr;
        }

        //如果时间不是今天、昨天，而是今年，则显示月日，比如 01-11
        String thisYear = DateFormatUtils.format(today, yyyyMM);
        String createTimeYear = DateFormatUtils.format(time, yyyyMM);
        if (thisYear.equals(createTimeYear)) {
            //通知类的消息都会在聊天页面展示，解析格式特殊处理
           /* if (MessageCodeEnum.NOTICE.getCode().equals(code)) {
                createTimeStr = DateFormatUtils.format(time.getTime(), MMdd_HHmm);
            } else {
                createTimeStr = DateFormatUtils.format(time.getTime(), MMDD);
            }*/
            return createTimeStr;
        }

        //到这里，时间肯定是去年及以上的了
        //通知类的消息都会在聊天页面展示，解析格式特殊处理
        /*if (MessageCodeEnum.NOTICE.getCode().equals(code)) {
            createTimeStr = DateFormatUtils.format(time.getTime(), yyyyMMdd_HHmm);
        }*/

        return createTimeStr;
    }

    public static String timestampToStr(Timestamp time, String format) {
        return new SimpleDateFormat(format).format(time);
    }

    public static Timestamp strToTimestamp(String time, String format) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        Date date = dateFormat.parse(time);
        Timestamp timestamp = new Timestamp(date.getTime());
        return timestamp;
    }

    public static String dateToStr(Date date, String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        return df.format(date);
    }

    public static String localDateToStr(LocalDate date, String format) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
        return df.format(date);
    }

    public static LocalDate strToLocalDate(String dateStr, String format) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
        return LocalDate.parse(dateStr, df);
    }

    /**
     * 转化日期成中文
     *
     * @param dateStr 日期字符串
     * @param type    类型：1-月 2-周
     * @return
     */
    public static String formatDateToChinese(String dateStr, int type) {
        if (type == 1) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
            YearMonth yearMonth = YearMonth.parse(dateStr, formatter);
            return yearMonth.getYear() + "年" + yearMonth.getMonthValue() + "月";
        } else if (type == 2) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date date = sdf.parse(dateStr);

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);

                int year = calendar.get(Calendar.YEAR);
                int month = calendar.get(Calendar.MONTH) + 1;
                int weekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH);

                return year + "年" + month + "月第" + weekOfMonth + "周";
            } catch (ParseException e) {
                return null;
            }
        }
        return null;
    }
}
